package edu.dublbo.app.habitplusfavorite.controller;

import edu.dublbo.app.habitplusfavorite.model.ResourceCatalog;
import edu.dublbo.app.habitplusfavorite.service.IResourceCatalogService;
import edu.dublbo.app.habitplusfavorite.service.IResourceService;
import edu.dublbo.app.habitplusfavorite.service.IVisitorLastHistoryService;
import edu.dublbo.app.habitplusfavorite.vo.ResourceVo;
import edu.dublbo.commons.util.IpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author habitplus
 * @since 2021/12/7 16:08
 */
@Controller
@Slf4j
public class ShowController {
    @Autowired
    private IResourceCatalogService resourceCatalogService;

    @Autowired
    private IResourceService resourceService;

    @Autowired
    private IVisitorLastHistoryService visitorLastHistoryService;

    @GetMapping(value = "/habitplus-favorite")
    public String show(HttpServletRequest request, Model model) {
        Map<String, Object> map = new HashMap<>();
        // 获取资源类别菜单树
        List<ResourceCatalog> menus = resourceCatalogService.listOfChildren();
        // 获取资源列表，按资源类别分组
        List<ResourceVo> resources = resourceService.listOfGroup();
        map.put("menus", menus);
        map.put("resources", resources);
        map.put("name", "habitplus");
        model.addAttribute("datas", map);

        /*
            处理页面流量统计
         */
        int port = request.getLocalPort(); // 本机此服务的实际端口号
        String ipAddr = IpUtil.getIpAddress(request);
//        log.info("请求的remoteHostIP={}", ipAddr);
        String url = IpUtil.getSelfIpAddress() + ":" + port + "/habitplus-favorite";
//        String url = request.getRequestURL().toString();
        visitorLastHistoryService.add(ipAddr, url);
        return "hello";
    }
}
